package com.nio.c3_Channel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @version No one is coming, unless you change yourself!!!
 * @author： pang-yun
 * @date： 2021-05-31 13:59
 */

public class ChannelTransferTo {
    public static void main(String[] args) {
        try (FileChannel from = new FileInputStream("data.txt").getChannel();
             FileChannel to = new FileOutputStream("to.txt").getChannel();
        ) {
            //transferTo中有三个参数  起始位置(0) ，长度(用size命令)，传输目标
            from.transferTo(0,from.size(),to);
            //效率高，底层会使用操作系统的零拷贝进行优化
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
